package protocol

import (
	"context"
	"fmt"
	"net/http"

	"gitee.com/go-kade/go12/vblog/ioc"
	"github.com/gin-gonic/gin"
)

// 封装一个自己的http server 优雅关闭程序
type HttpServer struct {
	server *http.Server
}

func NewHttpServer() *HttpServer {
	r := gin.Default()
	ioc.ApiHandler().RouteRegistry(r.Group("/api"))
	return &HttpServer{
		server: &http.Server{
			Addr:    "0.0.0.0:9999", // 服务监听的地址
			Handler: r,              // 监听关联的路由处理
		},
	}
}

func (s *HttpServer) Run() error {
	fmt.Printf("listen addr: %s\n", s.server.Addr)
	return s.server.ListenAndServe()
}

// 优雅关闭
func (s *HttpServer) Close(ctx context.Context) {
	s.server.Shutdown(ctx)
}
